/******************************************************************************/
* Authors: 	A.M. Birkenbach, M.-Y. Lee, & M.D. Smith
* Date: 	08/05/2023
* Project: 	Counterfactual Modeling of Multispecies Fisheries Outcomes Under 
*			Market-Based Regulation
* Purpose: 	Estimate structural parameters using alternative-specific cond logit
* Inputs:	data_for_discrete_choice_pre(20210925_172059).dta
*			data_for_discrete_choice_post(20210925_172059).dta
/******************************************************************************/

clear all
set more off

gl input ""
gl output ""

cap log close
qui log using "$output\1__DISCRETE_CHOICE_log.txt", replace text

set seed 1234
gl typevars1 "wkly_crew_wage len fuelprice fuelprice_len das_price_mean das_price_mean_len"
gl typevars2 "wkly_crew_wage len fuelprice fuelprice_len"
gl altvars1 "exp_rev_total fuelprice_distance distance mean_wind mean_wind_noreast permitted LApermit choice_prev_fish partial_closure start_of_season"
gl altvars2 "exp_rev_total_das fuelprice_distance distance mean_wind mean_wind_noreast permitted LApermit choice_prev_fish partial_closure start_of_season"



/**********************************/
/***** DESCRIPTIVE STATISTICS *****/
/**********************************/
gl typevars : list global(typevars1) | global(typevars2)
gl altvars : list global(altvars1) | global(altvars2)
di "$altvars $typevars"
foreach period in "pre" "post" {
	use "$input\data_for_discrete_choice_`period'(20210925_172059).dta", clear
	label var exp_rev_total "Total expected revenues under CS ($1000s)"
	label var exp_rev_total_das "Total expected revenues under DAS ($1000s)"
	*D-STATS AVERAGING ACROSS CHOICES
	cap erase "$output\d_stats_`period'.txt"
	foreach gear in "GILLNETS" "TRAWL" {
		estpost sum $typevars $altvars if gearcat=="`gear'" & spstock~="No_Fish"
		esttab using "$output\d_stats_`period'.txt", ///
			cells((count(label("N") fmt(%9.0f)) ///
			mean(label("Mean") fmt(%9.2f)) ///
			sd(label("Std. dev.") fmt(%9.2f)) ///
			min(label("Min.") fmt(%9.2f)) ///
			max(label("Max.") fmt(%9.2f)))) ///
			label noobs nonum tab title("`gear'") ///
			addnotes("" "") append
	}
}



/************************************/
/***** SUMMARIZE ACTUAL CHOICES *****/
/************************************/
use "$input\data_for_discrete_choice_pre(20210925_172059).dta", clear
append using "$input\data_for_discrete_choice_post(20210925_172059).dta"
collapse (sum) choice, by(post gearcat spstock)
gen choicefish=choice if spstock~="No_Fish"
reshape wide choice*, i(spstock gearcat)j(post)
forvalues i=0(1)1 {
	foreach name in "" "fish" {
		bysort gearcat: egen total`name'`i'=total(choice`name'`i')
		gen perc`name'`i'=round(100*(choice`name'`i'/total`name'`i'),.1)
		drop total`name'`i'
	}
}
drop choicefish*
order gearcat spstock choice0 perc0 percfish0 choice1 perc1 percfish1
replace spstock="zzNo_Fish" if spstock=="No_Fish"
sort gearcat spstock
replace spstock="No_Fish" if spstock=="zzNo_Fish"
replace spstock=subinstr(spstock,"_"," ",.)
replace gearcat=proper(gearcat)
label var gearcat "Gear"
label var choice0 "Choice count (pre)"
label var choice1 "Choice count (post)"
label var perc0 "Percent of all choices (pre)"
label var perc1 "Percent of all choices (post)"
foreach string in "CCGOM" "GB" "GOM" "SNEMA" {
	replace spstock=subinstr(spstock,"`str'","(`str')",1)
}
replace spstock="Red/Silver/Offshore Hake" if spstock=="Red Silver Offshore Hake"
replace spstock="Squid/Mackerel/Butterfish/Herring" if spstock=="Squid Mackerel Butterfish Herring"
format perc* %3.1f
qui export excel "$output\sum_actual_choices.xlsx", first(varl) replace

	
/***************************************************/
/***** ALTERNATIVE-SPECIFIC CONDITIONAL LOGITS *****/
/***************************************************/
cap timer clear
use "$input\data_for_discrete_choice_pre(20210925_172059).dta", clear
cmset id spstock2
forvalues i=1(1)2 {
	qui cap erase "$output\preCSasclogitnc`i'.ster"
	qui cap erase "$output\preCSasclogit`i'.ster"
}
		
forvalues i=1(1)2 {
	eststo clear
	foreach gear in "GILLNETS" "TRAWL" {
		*WITHOUT alt-specific constants
		eststo, title("pre`gear'`i'nc"): ///
		cmclogit choice ${altvars`i'} ///
			if gearcat=="`gear'", casevars(${typevars`i'}) ///
			base("NoFish") altwise vce(robust) nocons or
		qui estimates save "$output\preCSasclogitnc`i'.ster", append
		*WITH alt-specific constants
		eststo clear
		eststo, title("pre`gear'`i'"): ///
		cmclogit choice ${altvars`i'} ///
			if gearcat=="`gear'", casevars(${typevars`i'}) ///
			base("NoFish") altwise vce(robust) or
		qui estimates save "$output\preCSasclogit`i'.ster", append
	}
}
	

	
/**************************/
/***** EXPORT RESULTS *****/	
/**************************/
gl mlist ""
use "$input\data_for_discrete_choice_pre(20210925_172059).dta", replace
forvalues j=1(1)2 { // 1=gillnets, 2=trawl
	forvalues i=1(1)2 { // model #s
		foreach version in "" "nc" {
			est use "$output\\preCSasclogit`version'`i'.ster", number(`j')
			est store pre`version'`i'_`j'
			gl mlist "${mlist}pre`version'`i'_`j' "
		}
	}
}
di "$mlist"
qui estout $mlist using "$output\asclogits_ALL.txt", ///
	order() ///
	cells(b(star fmt(4)) se(par(`"="("' `")""') fmt(4)))  ///
	legend label varlabels(_cons Constant) ///
	stats(ll chi2 p N_case, fmt(2 4 4 0) ///
	labels("Log likelihood" "Chi-squared" "Significance" "Number of cases")) ///
	starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) replace transform(exp(@) exp(@))

qui log close


*end of do-file